package com.game.yxy.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.game.yxy.common.Result;
import com.game.yxy.entity.CusQ;
import com.game.yxy.entity.FdBack;
import com.game.yxy.mapper.FdBackMapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

@RestController
@RequestMapping("/fdback")
@Api(tags = "反馈管理") // 描述整个控制器的作用
public class FdBackController {
    @Resource
    FdBackMapper fdbackMapper;

    // 添加反馈
    @PostMapping
    @ApiOperation(value = "添加反馈", notes = "创建一个新的反馈记录")
    public Result<?> save(@RequestBody FdBack fdback) {
        fdbackMapper.insert(fdback);
        return Result.success();
    }

    // 更新反馈
    @PutMapping
    @ApiOperation(value = "更新反馈", notes = "根据ID更新反馈记录")
    public Result<?> update(@RequestBody FdBack fdback) {
        fdbackMapper.updateById(fdback);
        return Result.success();
    }

    // 删除反馈
    @DeleteMapping("/{id}")
    @ApiOperation(value = "删除反馈", notes = "根据ID删除反馈记录")
    public Result<?> delete(@ApiParam(value = "反馈ID", required = true) @PathVariable Long id) {
        fdbackMapper.deleteById(id);
        return Result.success();
    }

    // 查询反馈
    @GetMapping
    @ApiOperation(value = "查询反馈", notes = "支持姓名和关键字查询")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "50") Integer pageSize,
            @ApiParam(value = "姓名", defaultValue = "") @RequestParam(defaultValue = "") String name,
            @ApiParam(value = "搜索关键词", defaultValue = "") @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<FdBack> wrapper = Wrappers.<FdBack>lambdaQuery();
        if (StrUtil.isNotBlank(name)) {
            wrapper.like(FdBack::getName, name);
        }
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(FdBack::getFdback, search);
        }
        Page<FdBack> FdBackPage = fdbackMapper.selectPage(new Page<>(pageNum, pageSize), wrapper);
        return Result.success(FdBackPage);
    }

    // 根据ID查询反馈
    @GetMapping("/{id}")
    @ApiOperation(value = "根据ID查询反馈", notes = "根据反馈ID查询反馈记录")
    public Result<?> findById(@ApiParam(value = "反馈ID", required = true) @PathVariable Long id){
        FdBack fdBack = fdbackMapper.selectById(id);
        if (fdBack != null) {
            return Result.success(fdBack);
        } else {
            return Result.failure("反馈不存在");
        }
    }
}